package person.bangbang.im.Androidgin.UI;

import person.bangbang.im.Androidgin.AndroidginPref;
import person.bangbang.im.Androidgin.R;
import person.bangbang.im.Androidgin.Util.Log;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceChangeListener;


/**
 * a pref setting activity. if you want add extra prefs, 
 * please see the res/xml/preference.xml. a classifying is 
 * expected.
 * 
 * TODO when adding your prefs, adding default value setting 
 * in reset2default().
 * 
 * @author bangbang.song@gmail.com
 *
 */
public class PrefActivity extends PreferenceActivity 
						  implements OnPreferenceChangeListener {
	private final static String TAG = "PrefActivity";
	
	public final static String KEY_RESET = "pref_reset";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		addPreferencesFromResource(R.xml.preferences);
		
		((CheckBoxPreference)findPreference(KEY_RESET))
			.setOnPreferenceChangeListener(this);
	}
	public boolean onPreferenceClick(Preference preference) {
		// TODO Auto-generated method stub
		return reset2default();
	}
	
	/**
	 * 
	 * @return true always.
	 */
	private boolean reset2default() {
		// FIXME this does not work what i want, why???
		PreferenceManager.setDefaultValues(getApplicationContext(), R.xml.preferences, true);
		
		
		((CheckBoxPreference)findPreference(AndroidginPref.KEY_REM)).setDefaultValue(true);
		((CheckBoxPreference)findPreference(AndroidginPref.KEY_AUTO_LOGIN)).setDefaultValue(false);
		
		Log.d(TAG, "All reset to defautlt");
		return true; // update always.
	}
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		// TODO Auto-generated method stub
		return reset2default();
	}
}
